package com.yiren.dbaa.module.practice_set.line.bean;

import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yiren.backstage.currency.bean.LinePointBean;
import com.yiren.backstage.currency.entity.LineInfoEntity;
import com.yiren.backstage.db.dao.cache.DataCache;
import com.yiren.backstage.db.dao.cache.LineInfoCache;
import com.yiren.backstage.db.dao.cache.ProjectCache;
import com.yiren.backstage.db.dao.cache.interf.DataCacheStateCallBack;
import com.yiren.dbaa.currency.api.CommonCallBack;

import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Describe 线路合并
 *
 * @author WRX
 * @date 2022/11/21
 */
public class LineMerge {

    private Context context;
    private Gson gs;

    public LineMerge(Context context) {
        this.context = context;
        gs = new Gson();
    }

    /**
     * 合并全部的线路
     *
     * @param callBack
     */
    public void loadAllLineData(CommonCallBack callBack) {
        //加载数据缓存
        DataCache.refreshLineInfo(context, new DataCacheStateCallBack.Callback() {
            @Override
            public void success() {
                List<LineInfoEntity> allLineInfo = LineInfoCache.getAllLineInfo();
                for (LineInfoEntity lineInfo : allLineInfo) {
                    loadLineData(lineInfo, null);
                }
                callBack.success("");
            }
        });
    }

    /**
     * 合并当前线路数据
     *
     * @param lineInfo
     * @param callBack
     */
    public void loadLineData(LineInfoEntity lineInfo, CommonCallBack callBack) {
        List<LinePointBean> partnerLinePoints = null;
        List<LinePointBean> coachLinePoints = null;
        if (StringUtils.isNotEmpty(lineInfo.getPartneModifyVal())) {
            partnerLinePoints = gs.fromJson(lineInfo.getPartneModifyVal(), new TypeToken<List<LinePointBean>>() {
            }.getType());
        }
        if (StringUtils.isNotEmpty(lineInfo.getCoachModifyVal())) {
            coachLinePoints = gs.fromJson(lineInfo.getCoachModifyVal(), new TypeToken<List<LinePointBean>>() {
            }.getType());
        }
        if (null == partnerLinePoints) partnerLinePoints = new ArrayList<>();
        if (null == coachLinePoints) coachLinePoints = new ArrayList<>();
        configMerge(lineInfo, partnerLinePoints, coachLinePoints);
        if (null != callBack) {
            callBack.success("");
        }
    }

    /**
     * 合并数据
     *
     * @param lineInfo
     * @param partnerLinePoints
     * @param coachLinePoints
     */
    private void configMerge(LineInfoEntity lineInfo, List<LinePointBean> partnerLinePoints, List<LinePointBean> coachLinePoints) {
        if (partnerLinePoints.size() == 0 && coachLinePoints.size() == 0) {
            return;
        }
        if (partnerLinePoints.size() == 0) {
            //教练自采线路
            lineInfo.setLinePoints(coachLinePoints);
        } else {
            if (coachLinePoints.size() == 0) {
                //合伙人自采线路
                lineInfo.setLinePoints(partnerLinePoints);
            } else {
                List<LinePointBean> list = new ArrayList<>();
                for (int i = 0; i < partnerLinePoints.size(); i++) {
                    boolean hasPoint = false;
                    for (int j = 0; j < coachLinePoints.size(); j++) {
                        if (coachLinePoints.get(j).getLinePointId().equals(partnerLinePoints.get(i).getLinePointId())) {
                            list.add(coachLinePoints.get(j));
                            hasPoint = true;
                            break;
                        }
                    }
                    if (!hasPoint) list.add(partnerLinePoints.get(i));
                }
                lineInfo.setLinePoints(list);
            }
        }
        List<LinePointBean> linePoints = lineInfo.getLinePoints();
        for (int i = 0; i < linePoints.size(); i++) {
            LinePointBean linePointBean = linePoints.get(i);
            linePointBean.setProjectInfo(ProjectCache.getPointById(linePointBean.getPointId()));
        }
    }
}
